<template>
  <div id="manageQuestionView">
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <!-- 新增：内容列省略显示 -->
      <template #content="{ record }">
        <a-tooltip :content="record.content" position="top">
          <div class="content-cell">
            {{ record.content }}
          </div>
        </a-tooltip>
      </template>
      <!-- 新增：判题配置自定义显示 -->
      <template #judgeConfig="{ record }">
        <div v-if="record.judgeConfig">
          <div>
            <div>时间: {{ JSON.parse(record.judgeConfig).timeLimit }}ms</div>
            <div>内存: {{ JSON.parse(record.judgeConfig).memoryLimit }}MB</div>
            <div>堆栈: {{ JSON.parse(record.judgeConfig).stackLimit }}MB</div>
          </div>
        </div>
        <span v-else>无</span>
      </template>

      <!-- 判题用例显示（修改为每行两行） -->
      <template #judgeCase="{ record }">
        <div v-if="record.judgeCase">
          <div
            v-for="(testCase, index) in JSON.parse(record.judgeCase)"
            :key="index"
            style="margin-bottom: 8px"
          >
            <div>输入: {{ testCase.input }}</div>
            <div>输出: {{ testCase.output }}</div>
          </div>
        </div>
        <span v-else>无</span>
      </template>

      <!-- 4. 创建时间格式化 -->
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>

      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doUpdate(record)"> 修改</a-button>
          <a-button status="danger" @click="doDelete(record)">删除</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
} from "../../../generated/question";
import message from "@arco-design/web-vue/es/message";
import moment from "moment";
import { useRouter } from "vue-router";

const tableRef = ref();

const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  pageSize: 10,
  current: 1,
});

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};
/**
 * 监听searchParams改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

// {id: "1", title: "A+ D", content: "新的题目内容", tags: "["二叉树"]", answer: "新的答案", submitNum: 0,…}

const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "内容",
    slotName: "content",
    width: 150,
  },
  {
    title: "标签",
    dataIndex: "tags",
  },
  {
    title: "提交数",
    dataIndex: "submitNum",
  },
  {
    title: "通过数",
    dataIndex: "acceptedNum",
  },
  {
    title: "判题限制",
    slotName: "judgeConfig",
  },
  {
    title: "判题用例",
    slotName: "judgeCase",
  },
  {
    title: "用户id",
    dataIndex: "userId",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const doDelete = async (question: Question) => {
  const res = await QuestionControllerService.deleteQuestionUsingPost({
    id: question.id,
  });
  if (res.code === 0) {
    message.success("删除成功");
    loadData();
  } else {
    message.error("删除失败");
  }
};

const router = useRouter();

const doUpdate = (question: Question) => {
  router.push({
    path: "/update/question",
    query: {
      id: question.id,
    },
  });
};
</script>

<style scoped>
#manageQuestionView {
  :deep(.content-cell) {
    display: block;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    max-width: 150px; /* 与columns中设置的width一致 */
  }

  /* 确保表格布局固定 */

  :deep(.arco-table) {
    table-layout: fixed;
  }

  /* 单元格内边距调整 */

  :deep(.arco-table td) {
    padding: 8px 12px;
  }
}
</style>
